import axios from 'axios'

const baseUrl = "";
// const contentType = "application/x-www-form-urlencoded;charset=utf-8";
const contentType = "application/json;charset=utf-8";
const timeout = 60000;

axios.defaults.headers.post['Content-Type'] = contentType
axios.defaults.timeout = timeout

// 请求拦截
axios.interceptors.request.use(
    (config) => {
        config.url = baseUrl + config.url
        return config
    },
    (error) => {
        return Promise.reject(error)
    }
)

// 响应拦截
axios.interceptors.response.use(
    (response) => {
        if (response.status !== 200) {
            // code something
            return
        }
        return response.data
    },
    (error) => {
        return Promise.reject(error)
    }
)

export function baseRequest(url, data = null, method = 'POST', time = timeout) {
    let headers = {}
    headers = {
        'Content-Type': contentType,
    }
    axios.defaults.timeout = time

    if (method !== 'GET') {
        return axios.request({
            url,
            method,
            data: data,
            headers
        })
    } else {
        return axios.get(url, {
            params: data,
            headers
        })
    }
}

/**
 * get 请求
 * @param url 请求url(必填参数)
 * @param params 请求参数(非必填参数)，默认 null
 * @param headers 请求头(非必填参数)，默认 'Content-Type': "application/json;charset=utf-8";
 * @param time 超时时间(非必填参数)，默认 60000
 */
export function getRequest(url, params = null, headers = { 'Content-Type': contentType, }, time = timeout) {
    axios.defaults.timeout = time

    return axios.request({
        url,
        headers,
        method: "GET",
        params: params,
    })
}

/**
 * post 请求
 * @param url 请求url(必填参数)
 * @param params 请求参数(非必填参数)，默认 null
 * @param headers 请求头(非必填参数)，默认 'Content-Type': "application/json;charset=utf-8";
 * @param time 超时时间(非必填参数)，默认 60000
 */
export function postRequest(url, data = null, headers = { 'Content-Type': contentType, }, time = timeout) {
    axios.defaults.timeout = time

    return axios.request({
        url,
        headers,
        method: "POST",
        data: data,
    })
}

/**
 * delete 请求
 * @param url 请求url(必填参数)
 * @param params 请求参数(非必填参数)，默认 null
 * @param headers 请求头(非必填参数)，默认 'Content-Type': "application/json;charset=utf-8";
 * @param time 超时时间(非必填参数)，默认 60000
 */
export function deleteRequest(url, data = null, headers = { 'Content-Type': contentType, }, time = timeout) {
    axios.defaults.timeout = time

    return axios.request({
        url,
        headers,
        method: "DELETE",
        data: data,
    })
}

/**
 * update 请求
 * @param url 请求url(必填参数)
 * @param params 请求参数(非必填参数)，默认 null
 * @param headers 请求头(非必填参数)，默认 'Content-Type': "application/json;charset=utf-8";
 * @param time 超时时间(非必填参数)，默认 60000
 */
export function updateRequest(url, data = null, headers = { 'Content-Type': contentType, }, time = timeout) {
    axios.defaults.timeout = time

    return axios.request({
        url,
        headers,
        method: "UPDATE",
        data: data,
    })
}

/**
 * upload 请求
 * @param url 请求url(必填参数)
 * @param params 请求参数(非必填参数)，默认 null
 * @param headers 请求头(非必填参数)，默认 'Content-Type': "application/json;charset=utf-8";
 * @param time 超时时间(非必填参数)，默认 60000
 */
export function uploadRequest(url, data = null, headers = { 'Content-Type': "multipart/form-data", }, time = timeout) {
    axios.defaults.timeout = time

    return axios.request({
        url,
        headers,
        method: "POST",
        data: data,
    })
}